package com.health.viewscontrols;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.health.R;

public class SetServerIPActivity extends NovaActivity {

    private EditText ip_deviceA;
    private EditText ip_deviceB;
    private EditText ip_deviceC;
    private Button button_ok;
    private static String remoteIPA = "";
    private static String remoteIPB = "";
    private static String remoteIPC = "";

    private static String DB_NAME;
    private static final int DB_VERSION = 2;
    private static DatabaseHelper mOpenHelper;
    private static SQLiteDatabase db;

    public static String DB_CREATE_TABLE_IPCONFIG = "CREATE TABLE IF NOT EXISTS [setipconfig] " +
            "([ID] INTEGER PRIMARY KEY,[IP] VARCHAR)";

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DB_CREATE_TABLE_IPCONFIG);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }
    public void DbCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATE_TABLE_IPCONFIG);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Setup the window
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.ip_set);
        ip_deviceA = (IpEditer) findViewById(R.id.ip_deviceA);
        ip_deviceB = (IpEditer) findViewById(R.id.ip_deviceB);
        ip_deviceC = (IpEditer) findViewById(R.id.ip_deviceC);
        DB_NAME = "/sdcard/yishu/usingservice.db";
        mOpenHelper = new DatabaseHelper(this);
        db = mOpenHelper.getWritableDatabase();
        DbCreate(db);
        String sql = "select IP from setipconfig where ID=1;";
        Cursor cur;
        cur = db.rawQuery(sql, null);
        if (cur.moveToFirst()==true)
        {
            while (!cur.isAfterLast()) {
                remoteIPA = cur.getString(0).trim();
                break;
            }
        }
        cur.close();
        sql = "select IP from setipconfig where ID=2;";
        cur = db.rawQuery(sql, null);
        if (cur.moveToFirst()==true)
        {
            while (!cur.isAfterLast()) {
                remoteIPB = cur.getString(0).trim();
                break;
            }
        }
        cur.close();
        sql = "select IP from setipconfig where ID=3;";
        cur = db.rawQuery(sql, null);
        if (cur.moveToFirst()==true)
        {
            while (!cur.isAfterLast()) {
                remoteIPC = cur.getString(0).trim();
                break;
            }
        }
        cur.close();

        ip_deviceA.setText(remoteIPA);
        ip_deviceB.setText(remoteIPB);
        ip_deviceC.setText(remoteIPC);
        button_ok = (Button) findViewById(R.id.button_ok);
        button_ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                remoteIPA=ip_deviceA.getText().toString();
                String sql = "select IP from setipconfig where ID=1;";
                int p=0;
                Cursor cur;
                cur = db.rawQuery(sql, null);
                if (cur.moveToFirst()==true)
                {
                    while (!cur.isAfterLast()) {
                        p++;
                        break;
                    }
                }
                cur.close();

                if (p==0)
                {
                    sql= "insert into setipconfig (ID,IP) values(1,'"+remoteIPA+"');";
                    db.execSQL(sql);
                }else
                {
                    sql="update setipconfig set IP='"+remoteIPA+"'"+" where ID=1"+";";
                    db.execSQL(sql);
                }
                remoteIPB=ip_deviceB.getText().toString();
                sql = "select IP from setipconfig where ID=2;";
                p=0;
                cur = db.rawQuery(sql, null);
                if (cur.moveToFirst()==true)
                {
                    while (!cur.isAfterLast()) {
                        p++;
                        break;
                    }
                }
                cur.close();

                if (p==0)
                {
                    sql= "insert into setipconfig (ID,IP) values(2,'"+remoteIPB+"');";
                    db.execSQL(sql);
                }else
                {
                    sql="update setipconfig set IP='"+remoteIPB+"'"+" where ID=2"+";";
                    db.execSQL(sql);
                }
                remoteIPC=ip_deviceC.getText().toString();
                sql = "select IP from setipconfig where ID=3;";
                p=0;
                cur = db.rawQuery(sql, null);
                if (cur.moveToFirst()==true)
                {
                    while (!cur.isAfterLast()) {
                        p++;
                        break;
                    }
                }
                cur.close();

                if (p==0)
                {
                    sql= "insert into setipconfig (ID,IP) values(3,'"+remoteIPC+"');";
                    db.execSQL(sql);
                }else
                {
                    sql="update setipconfig set IP='"+remoteIPC+"'"+" where ID=3"+";";
                    db.execSQL(sql);
                }

                finish();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
        mOpenHelper.close();
    }

    @Override
    public void findViews() {

    }

    @Override
    public void setListeners() {

    }
}
